package edu.pku.sei.modeler.treeview;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.Viewer;

import edu.pku.sei.modeler.explorer.registry.ITreeViewContentProvider;
import edu.pku.sei.modeler.internal.MetaModelingProject;
import edu.pku.sei.modeler.model.common.MetaTypedList;
import edu.pku.sei.modeler.model.core.MetaClass;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.modeler.model.core.MetaPrimitiveType;

public class MetaModelContentProvider implements ITreeViewContentProvider {
	public Object[] getChildren(Object parent) {
		if (parent instanceof MetaModel) {
			MetaModel metaModel = (MetaModel) parent;
			return metaModel.getPackages().toArray();
		} 
		else if (parent instanceof MetaPackage) {
			MetaPackage metaPackage = (MetaPackage) parent;
			return getChildrenOfMetaPackage(metaPackage);
		}
		else if(parent instanceof MetaClass) {
			MetaClass cls = (MetaClass)parent;
			return getChildrenOfMetaClass(cls);
		}
		return new Object[0];
	}
	
	public Object getParent(Object child) {
		if(child instanceof MetaElement) {
			return ((MetaElement)child).getContainer();
		}
		return null;
	}

	private Object[] getChildrenOfMetaClass(MetaClass cls) {
		ArrayList<Object> list = new ArrayList<Object>();
		list.addAll(cls.getReferences());
		list.addAll(cls.getGenerals());
		return list.toArray();
	}
	protected Object[] getChildrenOfMetaPackage(MetaPackage metaPackage) {
		MetaTypedList subPackages = metaPackage.getSubPackages();
		MetaTypedList allNamedElements = metaPackage.getNamedElements();
		List namedElements = new ArrayList();
		for(int i=0; i<allNamedElements.size(); i++) {
			Object obj = allNamedElements.get(i);
			if(!(obj instanceof MetaPrimitiveType)) {
				namedElements.add(obj);
			}
		}
		MetaTypedList diagrams = metaPackage.getDiagrams();
		MetaTypedList otherElements = metaPackage.getOtherElements();
		
		int subPackagesSize = subPackages.size();
		int namedElementsSize = namedElements.size();
		int diagramsSize = diagrams.size();
		int otherElementsSize = otherElements.size();
		int totalSize = subPackagesSize + namedElementsSize +
						diagramsSize + otherElementsSize;
		
		Object[] children = new Object[totalSize];
		for (int i = 0; i < subPackagesSize; i++) {
			children[i] = subPackages.get(i);
		}
		for (int i = 0; i < namedElementsSize; i++) {
			children[subPackagesSize + i] = namedElements.get(i);
		}
		for (int i = 0; i < diagramsSize; i++) {
			children[subPackagesSize + namedElementsSize + i] = diagrams.get(i);
		}
		for (int i = 0; i < otherElementsSize; i++) {
			children[subPackagesSize + namedElementsSize + diagramsSize + i] = otherElements.get(i);
		}
		
		return children;		
	}

	@Override
	public String getNature() {
		return MetaModelingProject.PROJECT_NATURE;
	}

	@Override
	public boolean hasChildren(Object element) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		// TODO Auto-generated method stub
		
	}
}
